package com.yanqu.road.server.gameplayer.module.activity.tomb.cmd;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.tomb.Tomb;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.tomb.TombMgr;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = GameProtocol.S_TOMB_UPDATE_REPAIR_CONDITION,desc = "秦始皇修复同步到本服更新成就")
public class TombUpdateRepairConditionCmd extends AbstractCommand {

    private List<Long> userIdList;

    private Map<Integer,Long> progressMap;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        Tomb.TombUpdateUserRepairCondition msg = Tomb.TombUpdateUserRepairCondition.parseFrom(packet.getBytes());
        userIdList = msg.getUserIdList();
        progressMap = new ConcurrentHashMap<>();
        for (Tomb.TombRepairProgress progress:msg.getRepairDataList()){
            int maxProgressByRepairZoneId = TombMgr.getMaxProgressByRepairZoneId(progress.getId());
            if (maxProgressByRepairZoneId != 0){
                progressMap.put(progress.getId(),progress.getProgress()*100/ maxProgressByRepairZoneId);
            }else {
                progressMap.put(progress.getId(),0L);
            }
        }

        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        TombMgr.updateAllMemberRepairCondition(userIdList,progressMap);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
